/**
 *
 * @author 朱雀
 */
Ext.define('SvgEditor.graph.Anchor', {
	extend: 'SvgEditor.graph.Circle',
	onDDStart: function(x, y, e) {
		var anchor = this, canvas = anchor.canvas, model = canvas.selected[0];
		anchor.oldX = anchor.attrs.cx;
		anchor.oldY = anchor.attrs.cy;
		anchor.toBack();
		if (anchor.id === 'startP' || anchor.id === 'endP') {
			var flow = model, flowFrom = flow.flowFrom, flowTo = flow.flowTo, start, end;
			if (flowFrom) {
				start = [flowFrom.coreX, flowFrom.coreY];
			} else {
				start = flow.start;
			}
			if (flowTo) {
				end = [flowTo.coreX, flowTo.coreY];
			} else {
				end = flow.end;
			}
			flow.attr({path: [
					['M', start[0], start[1]],
					['L', end[0], end[1]]
				]});
			flow.start = start;
			flow.end = end;
		}
	},
	onDDMove: function(dx, dy, x, y, e) {
		var anchor = this, model = anchor.canvas.selected[0];
		anchor.attr({cx: anchor.oldX + dx, cy: anchor.oldY + dy});
		anchor.coreX = anchor.attrs.cx;
		anchor.coreY = anchor.attrs.cy;
		if (anchor.id === 'startP') {
			var flow = model;
			flow.attr({path: [
					['M', anchor.coreX, anchor.coreY],
					['L', flow.end[0], flow.end[1]]
				]});
		} else if (anchor.id === 'endP') {
			var flow = model;
			flow.attr({path: [
					['M', flow.start[0], flow.start[1]],
					['L', anchor.coreX, anchor.coreY]
				]});
		}
	},
	onDDEnd: function(x, y, e) {
		var anchor = this, canvas = anchor.canvas, model = canvas.selected[0];
		if (model.type === 'path') {
			var flow = model, flowPlate = flow.flowPlate, path = flow.attrs.path;
			flowPlate.attr({path: path});
			flowPlate.start = flow.start = [path[0][1], path[0][2]];
			flowPlate.end = flow.end = [path[1][1], path[1][2]];
			if (anchor.id === 'startP') {
				canvas.connect(flow, canvas.target, null, true);
			}
			if (anchor.id === 'endP') {
				canvas.connect(flow, null, canvas.target, true);
			}
			canvas.viewAnchor(canvas, flow);
		}
	}
});